package com.example.myui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myui.R;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import com.yuyh.library.imgsel.config.ISCameraConfig;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.List;

import me.shaohui.bottomdialog.BaseBottomDialog;
import me.shaohui.bottomdialog.BottomDialog;

public class GalleryActivity extends AppCompatActivity implements View.OnClickListener {

    private BaseBottomDialog bottomDialog;
    private String TAG = GalleryActivity.class.getSimpleName();
    private static final int REQUEST_CAMERA_CODE = 1001;//拍照
    private static final int REQUEST_LIST_CODE = 1002;//选图片
    private TextView tvResult;
    private ImageView imageview;
    private ImageView imageview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        initView();

    }

    private void initView() {
        imageview = findViewById(R.id.iv_result_pic);
        imageview1 = findViewById(R.id.iv_result_pic1);
        tvResult = findViewById(R.id.tv_result);
        Button takePic = findViewById(R.id.takephoto);
        takePic.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.takephoto:

                // 自定义图片加载器
                ISNav.getInstance().init(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, String path, ImageView imageView) {
                        Glide.with(context).load(path).into(imageView);
                    }
                });

                bottomDialog = BottomDialog.create(getSupportFragmentManager())
                        .setViewListener(new BottomDialog.ViewListener() {
                            @Override
                            public void bindView(View v) {
                                v.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        bottomDialog.dismiss();
                                    }
                                });

                                v.findViewById(R.id.iv_takephoto).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ISCameraConfig config = new ISCameraConfig.Builder()
                                                .needCrop(true) // 裁剪
                                                .cropSize(1, 1, 200, 200)
                                                .build();

                                        ISNav.getInstance().toCameraActivity(GalleryActivity.this, config, REQUEST_CAMERA_CODE);
                                    }
                                });

                                v.findViewById(R.id.iv_selectpic).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // 自由配置选项
                                        ISListConfig config = new ISListConfig.Builder()
                                                // 是否多选, 默认true
                                                .multiSelect(true)
                                                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                                                .rememberSelected(false)
                                                // “确定”按钮背景色
                                                .btnBgColor(Color.GRAY)
                                                // “确定”按钮文字颜色
                                                .btnTextColor(Color.BLUE)
                                                // 使用沉浸式状态栏
                                                .statusBarColor(Color.parseColor("#3F51B5"))
                                                // 返回图标ResId
                                                .backResId(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material)
                                                // 标题
                                                .title("图片")
                                                // 标题文字颜色
                                                .titleColor(Color.WHITE)
                                                // TitleBar背景色
                                                .titleBgColor(Color.parseColor("#3F51B5"))
                                                // 裁剪大小。needCrop为true的时候配置
                                                .cropSize(1, 1, 200, 200)
                                                .needCrop(true)
                                                // 第一个是否显示相机，默认true
                                                .needCamera(true)
                                                // 最大选择图片数量，默认9
                                                .maxNum(9)
                                                .build();

                                        // 跳转到图片选择器
                                        ISNav.getInstance().toListActivity(GalleryActivity.this, config, REQUEST_LIST_CODE);


                                    }
                                });

                            }
                        })
                        .setLayoutRes(R.layout.bottom_layout)
                        .setDimAmount(0.1f)            // Dialog window dim amount(can change window background color）, range：0 to 1，default is : 0.2f
                        .setCancelOutside(false)     // click the external area whether is closed, default is : true
                        .setTag("BottomDialog")     // setting the DialogFragment tag
                        .show();

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CAMERA_CODE && resultCode == RESULT_OK && data != null) {
            String path = data.getStringExtra("result"); // 图片地址
            tvResult.append(path + "\n");
            Glide.with(GalleryActivity.this)
                    .load(path)
                    .into(imageview);
        } else if (requestCode == REQUEST_LIST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra("result");
            for (String path : pathList) {
                tvResult.append(path + "\n");
            }
            Log.i(TAG, "onActivityResult: "+ pathList.get(0));
            Glide.with(GalleryActivity.this)
                    .load(pathList.get(0))
                    .into(imageview1);
        }
    }

}
